package com.itheima.dao.system;

import com.itheima.domain.system.Dept;
import com.itheima.domain.system.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 平台系统管理 --> 用户管理(数据层)
 */
public interface UserDao {
    @Select("select * from ss_user")
    @Results(id="userMap",value = {
            @Result(column = "user_id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "user_name", property = "userName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.DECIMAL),
            @Result(column = "gender", property = "gender", javaType = String.class),
            @Result(column = "telephone", property = "telephone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "birthday", property = "birthday", jdbcType = JdbcType.VARCHAR),
            @Result(column = "join_date", property = "joinDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "dept_id", property = "deptId", jdbcType = JdbcType.VARCHAR),
            @Result(
                    property = "dept",
                    column = "dept_id",
                    javaType = Dept.class,
                    one = @One(select = "com.itheima.dao.system.DeptDao.findById")
            )
    })
    List<User> findAll() throws Exception;

    @Select("select * from ss_user where user_id = #{id}")
    @ResultMap("userMap")
    User findById(String id) throws Exception;


    /**
     * 新增用户
     */
    @Insert("insert into ss_user(user_id,email,user_name,password,state,gender,telephone,birthday,join_date,dept_id) values(#{id},#{email},#{userName},#{password},#{state},#{gender},#{telephone},#{birthday},#{joinDate},#{deptId})")
    int save(User user) throws Exception;

    /**
     * 更新用户
     */
    @Update("update ss_user set email=#{email}, user_name=#{userName}, state=#{state}, gender=#{gender}, telephone=#{telephone}, birthday=#{birthday}, join_date=#{joinDate}, dept_id=#{deptId} where user_id=#{id}")
    int update(User user) throws Exception;

    /**
     * 根据ID删除用户
     */
    @Delete("delete from ss_user where user_id = #{id}")
    int deleteById(String id) throws Exception;

}
